import moment from 'moment'

moment.locale('zh_CN')

export default {

  getCurrentPosition: function (cb) {
    new BMap.Geolocation().getCurrentPosition(function (r) {
      if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        let location = {
          longitude: r.point.lng,
          latitude: r.point.lat,
          city: r.address.city.replace('市', '')
        };
        sessionStorage.setItem('city', location.city);
        sessionStorage.setItem('longitude', location.longitude);
        sessionStorage.setItem('latitude', location.latitude);
        cb & cb(location)

      }	else {
        alert('failed'+this.getStatus());
      }   
    })
  },
  isD11: function () {
    if ((!sessionStorage.getItem('city') || sessionStorage.getItem('city') === '广州') && Date.parse(new Date()) < new Date('2017/11/11 07:10:00').getTime()) {
      return false;
    } else {
      return false;
    }
  },
  getQueryString: function (name) {
    var r = window.location.href.match(new RegExp("(\\?|&)" + name + "=([^&#]*)(&|#|$)", "i"));
    if (r) {
      return r[2];
    }
    return null;
  },
  setCurrentUrl: function () {
    // 删除 url 中手动添加的随机数 wxr
    let url = location.href.replace(/&wxr=[0-9]{13}/g, '');
    window.history.replaceState({}, document.title, url);
  },
  formatTime: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD H:mm:ss')
  },
  formatDate: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD')
  },
  MillisecondToDate: function (msd) {
    var time = parseFloat(msd) / 1000;
    if (null != time && "" != time) {
      if (time > 60 && time < 60 * 60) {
        let m = parseInt(time / 60.0);
        let s = parseInt((parseFloat(time / 60.0) -
          parseInt(time / 60.0)) * 60);
        time = '00:' + (m > 9 ? '' : '0') + m + ":" + (s > 9 ? '' : '0') + s;
      } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
        let h = parseInt(time / 3600.0);
        let m = parseInt((parseFloat(time / 3600.0) -
          parseInt(time / 3600.0)) * 60);
        let s = parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) - parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60);
        time = (h > 9 ? '' : '0') + h + ":" + (m > 9 ? '' : '0') + m + ":" + (s > 9 ? '' : '0') + s;
      } else {
        time = "00:00:" + parseInt(time);
      }
    } else {
      time = "00:00:00";
    }
    return time;

  },
  firstDayOfMonth: function () {
    return moment().startOf('month');
  },
  endDayOfMonth: function () {
    return moment().endOf('month')
  },

  currentYear: function () {
    return moment().year()
  },

  currentMonth: function () {
    return moment().month() + 1
  },

  formatNumber: function (val, fixed) {
    return parseFloat(val).toFixed(fixed)
  },
  formatDatas: function (num) {
    let str = num + '';
    let value = '';
    let ints = str;
    if (str.indexOf(".") > -1) {
      let strArr = str.split(".");
      ints = strArr[0];
      let detail = strArr[1];

      if (parseInt(detail) == 0) {
        return ints
      } else {
        return num
      }
    } else {
      return num
    }
  },
  isNumber: function (val) {
    if (val == '') {
      return '';
    }
    if (!(/(^[1-9]\d*$)/.test(val))) {
      return '';
    } else {
      return val
    }

  },
  toNumber: function (val) {
    return Number(val)
  },

  isEmptyObject: function (e) {
    var t;
    for (t in e)
      return !1;
    return !0
  },
  encodeUrl: function (url) {
    return encodeURI(url);
  },
  isWeixin: function () {
    let ua = navigator.userAgent.toLowerCase();
    return ua.indexOf('micromessenger') != -1;
  },

  isIOS: function () {
    var u = navigator.userAgent
    return u.indexOf('iPhone') > -1
  },

  getSysCode: function (value) {
    var res;
    let switchCityToSys = {
      '564701030634225664': '0002',
      '565541463497113600': '0004',
      '599411894062678016': '0003',
      '607495955062718464l': "0003",
      '1027114332118241280': '1027114332118241280',

    }
    for (let key in switchCityToSys) {
      console.log(key)
      if (key === value) {
        res = switchCityToSys[key]
      }
    }
    return res
  },
  /**
   * sortArray
   * @description 对数组进行排序，可用 [{},{}] 或者 [xx,xx]
   * @param {*} name 数组里比较对象的key，没有可以传 ''
   * @param {*} way  控制升序还是降序，(asc-升序 des-降序）,默认升序
   */
  sortArray(name, way) {
    return function (o, p) {
      var a, b;
      let newa, newb;
      way = way || 'asc'
      if (typeof o === "object" && typeof p === "object" && o && p) {
        a = o[name]
        b = p[name]
        newa = a.toString().toLowerCase()
        newb = b.toString().toLowerCase()
        if (newa === newb) {
          return 0;
        }
      } else if (o && p) {
        newa = o.toString().toLowerCase()
        newb = p.toString().toLowerCase()
        if (newo === newp) {
          return 0;
        }
      }
      if (way === 'asc') {
        return newa > newb ? 1 : -1;
      } else if (way === 'des') {
        return newa > newb ? -1 : 1;
      }
    }
  }
}
